package com.springboot.ww.sharding.controller;


import com.springboot.ww.sharding.dto.Condition;
import com.springboot.ww.sharding.dto.Query;
import com.springboot.ww.sharding.entity.PackageHis;
import com.springboot.ww.sharding.mapper.PackageHisMapper;
import lombok.AllArgsConstructor;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2025-07-04 11:39
 */
@AllArgsConstructor
@RestController
@RequestMapping("/dynamic")
public class TestController {

    private final PackageHisMapper packageHisMapper;


    @GetMapping("/test")
    @Transactional
    public R<String> test(Query query, String packageCode) {
        List<PackageHis> packageHis = packageHisMapper.selectPackageHisPage(Condition.getPage(query), packageCode, null);
        System.out.println(JsonUtil.toJson(packageHis.stream().map(PackageHis::getId).collect(Collectors.toList())));
        List<PackageHis> packageHis2 = packageHisMapper.selectByPackageCode(packageCode);
        System.out.println(JsonUtil.toJson(packageHis2.stream().map(PackageHis::getId).collect(Collectors.toList())));
        return R.data("测试成功");
    }

    @GetMapping("/test2")
    public R<String> test2(Query query, String packageCode) {
        List<PackageHis> packageHis = packageHisMapper.selectPackageHisPage(Condition.getPage(query), packageCode, null);
        System.out.println(JsonUtil.toJson(packageHis.stream().map(PackageHis::getId).collect(Collectors.toList())));
        HintManager hintManager = HintManager.getInstance();
        //强制主库 任何场景都能生效
        //hintManager.setWriteRouteOnly();
        //分片场景才会生效
        hintManager.addDatabaseShardingValue("logic_db", "master");
        //hintManager.addTableShardingValue("logic_db", "master");
        List<PackageHis> packageHis2 = packageHisMapper.selectByPackageCode(packageCode);
        System.out.println(JsonUtil.toJson(packageHis2.stream().map(PackageHis::getId).collect(Collectors.toList())));
        return R.data("测试成功");
    }

    @GetMapping("/test3")
    @Transactional
    @ShardingSphereTransactionType(TransactionType.XA)
    public R<String> test3(Query query, String packageCode) {
        List<PackageHis> packageHis = packageHisMapper.selectPackageHisPage(Condition.getPage(query), packageCode, null);
        System.out.println(JsonUtil.toJson(packageHis.stream().map(PackageHis::getId).collect(Collectors.toList())));
        HintManager hintManager = HintManager.getInstance();
        //强制主库 任何场景都能生效
        //hintManager.setWriteRouteOnly();
        //分片场景才会生效
        hintManager.addDatabaseShardingValue("logic_db", "master");
        //hintManager.addTableShardingValue("logic_db", "master");
        List<PackageHis> packageHis2 = packageHisMapper.selectByPackageCode(packageCode);
        System.out.println(JsonUtil.toJson(packageHis2.stream().map(PackageHis::getId).collect(Collectors.toList())));
        return R.data("测试成功");
    }
}
